<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Users as M;
use app\admin\model\Rules;
use app\admin\model\Menus;
use app\common\model\Areas;

class Users extends Controller
{
    public function index()
    { 
        $m = new M();
        $rs = $m->getAll();
        $this->assign('rs', $rs);        
        return $this->fetch();
    }
    
    public function dlbrowse()
    {
        return $this->fetch();
    }

    /**
     *  检查登录
     */
    public function checkLogin()
    { 
        $m = new M();
        return $m->checkLogin();
    }
    /**
     *  添加页面
     */    
    public function add()
    {
        $m         = new Rules();
        $rules     = $m->getAll();
        $mi        = new \app\common\model\IconTypes();
        $icontypes = $mi->getAllTypes();
        $modes     = \app\common\model\IconTypes::getMode();

        return $this->fetch('',compact('icontypes','rules','modes'));
    }
    /**
     *  添加操作
     */
    public function add_submit()
    { 
        $m = new M();
        return $m->add();
    }    
    /**
     *  修改页面
     */
    public function edit()
    {
        $m         = new Rules();
        $rules     = $m->getAll();
        $user      = new M();
        $s         = $user->getById(input('id'));
        $mi        = new \app\common\model\IconTypes();
        $icontypes = $mi->getAllTypes();
        $this->assign('id', $s->id);
        $this->assign('s', $s);
        $this->assign('rules', $rules);
        $this->assign('modes', \app\common\model\IconTypes::getMode());
        $this->assign('icontypes', $icontypes);
        return $this->fetch();
    }   
    /**
     *  修改操作
     */
    public function edit_submit()
    { 
        $m = new M();
        return $m->edit();
    } 
    /**
     *  删除操作
     */
    public function del_submit()
    { 
        $m = new M();
        $m->del();
        $id = input('id');
        $name = session('my_self')['name'];
        $des = $name."【删除】了编号为【".$id."】的用户";
        add_log(2,session('my_self')['id'], $des);
        $this->success('新增成功', 'User/');
    }
    /**
     *  列表回去josn数据
     */
    public function getuser_josn()
    {
        $m     = new M();
        $menus = $m->getAll("type=1");
        $lists = ['code' => 0, 'msg' => '', 'data' => []];
        foreach ($menus as $k => $v)
        {
            $lists['data'][] = $v;
        }
        return $lists;
    }
    /**
     *  代理josn
     */
    public function getdl_josn()
    {
        $m     = new M();
        $user = $m->getAll("type=2");
        $lists = ['code' => 0, 'msg' => '', 'data' => []];
        foreach ($user as $k => $v)
        {
            $v['area'] = $v['a_sheng'].$v['a_shi'];
            $lists['data'][] = $v;
        }
        return $lists;
    }
    
    public function dladd()
    {
        $Area = new Areas();
        $m = new Rules();
        $rules = $m->getAll();
        $this->assign('rules', $rules);
        $rs = $Area->getAll("status = 1");
        $this->assign('rs', $rs);
        if(request()->isPost())
        {
            if (input('post.user') == '')
            {
                return js_return(0, "账号不能为空");
            }
            if (input('post.name') == '')
            {
                return js_return(0, "昵称不能为空");
            }
            if (input('post.pwd') == '')
            {
                return js_return(0, "密码不能为空");
            }
            
            $rule = input('post.rules/a');
            if(empty($rule))
            {
                return js_return(0, "请至少选择一个角色");
            }
            
            $a = $Area->getById(input("a_id"));
            $secretKey      = get_secret_key();
            $U = new M();
            $opt             = new \stdClass;
            $opt->user       = input('post.user');
            $opt->name       = input('post.name');
            $opt->pwd        = md5(input('post.pwd') . md5($secretKey));
            $opt->sort       = input('post.sort');
            $opt->type       = 2;
            $opt->a_id       = input('a_id');
            $opt->a_sheng    = $a->sheng;
            $opt->a_shi      = $a->shi;
            $opt->secret_key = $secretKey;
            $opt->rules      = implode(',', input('post.rules/a'));
            $result          = $U->save($opt);
            //日志
            $id             = $U->order("id desc")->find();
            $name = session('my_self')['name'];
            $des = $name."【添加】了编号为【".$id->id."】的区域代理";
            add_log(2,session('my_self')['id'], $des);
            if (false !== $result)
            {
                return js_return(1, "添加成功");
            }
            else
            {
                return js_return(0, $this->getError());
            }
        }
        return $this->fetch();
    }
    public function dledit()
    {
        $U = new M();
        $s = $U->getById(input('id'));
        $this->assign('s', $s);
        $Area = new Areas();
        $m = new Rules();
        $rules = $m->getAll();
        $this->assign('rules', $rules);
        $rs = $Area->getAll("status = 1");
        $this->assign('rs', $rs);
        if(request()->isPost())
        {
            if (input('post.user') == '')
            {
                return js_return(0, "账号不能为空");
            }
            if (input('post.name') == '')
            {
                return js_return(0, "昵称不能为空");
            }
            
            $rule = input('post.rules/a');
            if(empty($rule))
            {
                return js_return(0, "请至少选择一个角色");
            }
            
            $a = $Area->getById(input("a_id"));
            $U = new M();
            $opt             = new \stdClass;
            $opt->user       = input('post.user');
            $opt->name       = input('post.name');
            $opt->sort       = input('post.sort');
            $opt->type       = 2;
            $opt->a_id       = input('a_id');
            $opt->a_sheng    = $a->sheng;
            $opt->a_shi      = $a->shi;
            $opt->rules      = implode(',', input('post.rules/a'));
            if(input('post.pwd') !='')
            {
                $secretKey      = get_secret_key();
                $opt->pwd       = md5(input('post.pwd') . md5($secretKey));
                $opt->secret_key = $secretKey;
            }

            $result          = $U->update($opt, 'id=' . input('id'));
            //日志
            $id = input('post.id');
            $name = session('my_self')['name'];
            $des = $name."【修改】了编号为【".$id."】的用户";
            add_log(2,session('my_self')['id'], $des);
            if (false !== $result)
            {
                return js_return(1, "更新成功");
            }
            else
            {
                return js_return(0, $this->getError());
            }
        }
        return $this->fetch();
    }
    
    
}
